<template>
  <el-card shadow="never"
           class="aui-card--fill">
    <div class="mod-sys__user">
      <el-form :inline="true"
               :model="dataForm"
               @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="dataForm.username"
                    @clear="getDataList()"
                    :placeholder="$t('user.username')"
                    clearable></el-input>
        </el-form-item>
        <!-- <el-form-item>
          <ren-select v-model="dataForm.gender"
                      dict-type="gender"
                      :placeholder="$t('user.gender')"></ren-select>
        </el-form-item> -->
        <el-form-item>
          <el-input v-model="dataForm.realName"
                    @clear="getDataList()"
                    :placeholder="$t('user.realName')"
                    clearable></el-input>

          <!-- <ren-dept-tree v-model="dataForm.deptId"
                         :placeholder="$t('dept.title')"
                         :query="true"></ren-dept-tree> -->
        </el-form-item>
        <!-- <el-form-item>
          <select-organization-input @resetSearch="changeDeptId($event)" />
        </el-form-item> -->
        <el-form-item>
          <el-button @click="getDataList()"
                     size="small">{{ $t('query') }}</el-button>
        </el-form-item>
        <el-form-item v-if="$hasPermission('sys:user:save')">
          <el-button type="primary"
                     size="small"
                     @click="addOrUpdateHandle()">{{ $t('add') }}</el-button>
        </el-form-item>
        <el-form-item v-if="$hasPermission('sys:user:delete')">
          <el-button type="danger"
                     size="small"
                     @click="deleteHandle()">{{ $t('deleteBatch') }}</el-button>
        </el-form-item>
        <!-- <el-form-item v-if="$hasPermission('sys:user:export')">
          <el-button type="info"
                     size="small"
                     @click="exportHandle()">{{ $t('export') }}</el-button>
        </el-form-item> -->
        <el-form-item v-if="$hasPermission('sys:user:disabled')">
          <el-button type="danger"
                     size="small"
                     @click="disabledBatch()">{{ $t('custom.sys.user.disabledBatch') }}</el-button>
        </el-form-item>
        <el-form-item v-if="$hasPermission('sys:user:enabled')">
          <el-button type="success"
                     size="small"
                     @click="enabledBatch()">{{ $t('custom.sys.user.enabledBatch') }}</el-button>
        </el-form-item>
        <!-- <el-form-item v-if="$hasPermission('sys:user:syncteamsuser')">
          <el-button type="warning"
                     size="small"
                     @click="syncTeamsUser()">{{ $t('custom.sys.user.syncTeamsUser') }}</el-button>
        </el-form-item>
        <el-form-item v-if="$hasPermission('sys:user:syncteamsuser')">
          <el-button type="primary"
                     size="small"
                     @click="disablednonCwUser">{{ $t('custom.sys.user.disablednonCwUser') }}</el-button>
        </el-form-item> -->
      </el-form>
      <el-table v-loading="dataListLoading"
                :data="dataList"
                border
                @selection-change="dataListSelectionChangeHandle"
                @sort-change="dataListSortChangeHandle"
                style="width: 100%;">
        <el-table-column type="selection"
                         header-align="center"
                         align="center"
                         width="50"></el-table-column>
        <el-table-column prop="username"
                         :label="$t('user.username')"
                         sortable="custom"
                         header-align="center"
                         align="left"
                         width="350"></el-table-column>
        <el-table-column prop="realName"
                         :label="$t('user.realName')"
                         header-align="center"
                         align="left"></el-table-column>
        <!-- <el-table-column prop="firstName"
                         :label="$t('user.firstName')"
                         header-align="center"
                         align="left"></el-table-column> -->
        <!-- <el-table-column prop="lastName"
                         :label="$t('user.lastName')"
                         header-align="center"
                         align="left"></el-table-column> -->
        <!--<el-table-column prop="mobile"
                         :label="$t('user.mobile')"
                         sortable="custom"
                         header-align="center"
                         align="left"></el-table-column>-->
        <!-- <el-table-column prop="managerName"
                         :label="$t('user.managerName')"
                         sortable="custom"
                         header-align="center"
                         align="left"></el-table-column> -->
        <!-- <el-table-column prop="gender"
                         :label="$t('user.gender')"
                         sortable="custom"
                         header-align="center"
                         align="center">
          <template slot-scope="scope">
            {{ $getDictLabel("gender", scope.row.gender) }}
          </template>
        </el-table-column> -->
        <el-table-column prop="status"
                         :label="$t('user.status')"
                         sortable="custom"
                         header-align="center"
                         align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === 0"
                    size="small"
                    type="danger">{{ $t('user.status0') }}</el-tag>
            <el-tag v-else
                    size="small"
                    type="success">{{ $t('user.status1') }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="createDate"
                         :label="$t('user.createDate')"
                         sortable="custom"
                         header-align="center"
                         align="center"
                         width="180">
          <template slot-scope="scope">
            {{ timeAreaFormat(scope.row.createDate, $store.state.user.timezoneId) }}
          </template>
        </el-table-column>
        <el-table-column prop="updateDate"
                         :label="$t('user.updateDate')"
                         sortable="custom"
                         header-align="center"
                         align="center"
                         width="180">
          <template slot-scope="scope">
            {{ timeAreaFormat(scope.row.updateDate, $store.state.user.timezoneId) }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('handle')"
                         fixed="right"
                         header-align="center"
                         align="center"
                         width="150">
          <template slot-scope="scope">
            <el-button v-if="$hasPermission('sys:user:update')"
                       type="text"
                       size="small"
                       @click="addOrUpdateHandle(scope.row.id)">{{ $t('update') }}</el-button>
            <el-button v-if="$hasPermission('sys:user:delete')"
                       type="text"
                       size="small"
                       @click="deleteHandle(scope.row.id)">{{ $t('delete') }}</el-button>
            <el-button v-if="$hasPermission('sys:user:delete')"
                       type="text"
                       size="small"
                       @click="resetPassword(scope.row)">{{ $t('resetPassword') }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination :current-page="page"
                     :page-sizes="[10, 20, 50, 100]"
                     :page-size="limit"
                     :total="total"
                     layout="total, sizes, prev, pager, next, jumper"
                     @size-change="pageSizeChangeHandle"
                     @current-change="pageCurrentChangeHandle">
      </el-pagination>
      <add-or-update v-if="addOrUpdateVisible"
                     ref="addOrUpdate"
                     @refreshDataList="getDataList"></add-or-update>
      <reset-password v-if="resetPasswordVisible"
                      ref="reset-password"
                      @refreshDataList="getDataList"></reset-password>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './user-add-or-update'
import SelectOrganizationInput from './user-select-dept'
import moment from 'moment'
import ResetPassword from '../../mspbots/client-portal/components/reset-password'
import { useMsal } from '@/components/storybook/hooks/useMsal'
const { getUserAuthorization } = useMsal()

export default {
  mixins: [mixinViewModule],
  data () {
    return {
      mixinViewModuleOptions: {
        getDataListURL: '/web/um/sys/user/page',
        getDataListIsPage: true,
        deleteURL: '/web/um/sys/user',
        deleteIsBatch: true,
        exportURL: '/web/um/sys/user/export',
        disabledBatchURL: '/web/um/sys/user/batch/status',
        disabledIsBatch: true
        // ,disablednonCwUserURL: '/web/um/sys/user/disableUserNoCW'
      },
      dataForm: {
        username: '',
        deptId: '',
        gender: '',
        realName: '',
        regSource: 2
      },
      resetPasswordVisible: false
    }
  },
  components: {
    AddOrUpdate, SelectOrganizationInput, ResetPassword
  },
  methods: {
    syncTeamsUser () {
      getUserAuthorization(this.$store.state.user.tenantCode)
    },
    changeDeptId (deptList) {
      deptList && deptList.length && (this.dataForm.deptId = deptList[0])
      this.query()
    },
    resetPassword (data) {
      this.resetPasswordVisible = true
      this.$nextTick(() => {
        this.$refs['reset-password'].dataForm.userId = data.id
        this.$refs['reset-password'].dataForm.username = data.username
        this.$refs['reset-password'].init()
      })
    },
    timeAreaFormat (time, timezone) {
      if (time) {
        return moment.tz(time, timezone).format('MM/DD HH:mm')
      } else {
        return time
      }
    },
    disablednonCwUser () {
      if (this.$store.state.user.superAdmin === 1) {
        return this.$message.error(this.$t('custom.sys.user.superadminNoCW'))
      }
      this.$confirm(this.$t('prompt.para', { 'handle': this.$t('custom.sys.user.disablednonCwUsertext') }), this.$t('prompt.title'), {
        confirmButtonText: this.$t('confirm'),
        cancelButtonText: this.$t('cancel'),
        type: 'warning'
      }).then(() => {
        this.dataListLoading = true
        const params = {
          tenantCode: this.$store.state.user.tenantCode
        }
        this.$interface.system.disablednonCwUser(params).then(({ data: res }) => {
          this.dataListLoading = false
          if (res.code !== 0) {
            this.dataList = []
            this.total = 0
            return this.$message.error(res.msg)
          }
          this.dataList = this.mixinViewModuleOptions.getDataListIsPage ? res.data.list : res.data
          this.total = this.mixinViewModuleOptions.getDataListIsPage ? res.data.total : 0
        }).catch(() => {
          this.dataListLoading = false
        })
      }).catch(() => { })
    }
  }
}
</script>
